package Machine;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Simplified management of IOR textual files: exit upon error.
 * 
 * @author perennou
 */
public class IO {

	public static void writeIOR(String textualIOR, String filename) {
		// write textual IOR to file
		try {
			PrintWriter out = new PrintWriter(new FileWriter(System.getProperty("user.dir")+System.getProperty("file.separator")+"IOR"+System.getProperty("file.separator")+filename));
			System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+"IOR"+System.getProperty("file.separator")+filename);
			out.println(textualIOR);
			out.close();
		} catch (IOException e) {
			System.out.println("Cannot write IOR to " + filename + ": " + e);
			System.exit(1);
		}
	}
	
	
	public static String readIOR(String filename) {
		// Read file content
		String textualIOR = null;
		try {
			BufferedReader in = new BufferedReader(new FileReader(System.getProperty("user.dir")+System.getProperty("file.separator")+"www"+System.getProperty("file.separator")+"Local"+System.getProperty("file.separator")+filename));
			textualIOR = in.readLine();
			in.close();
		}  catch (IOException e) {
			System.out.println("Cannot read IOR from " + filename + ": " + e);
			System.exit(1);
		}
		return textualIOR;
	}
}
